package com.netmi.myjectpack.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import com.netmi.myjectpack.R;
import com.netmi.myjectpack.bean.UserInfo;
import com.netmi.myjectpack.databinding.FragmentStockLivedataBinding;
import com.netmi.myjectpack.viewmodel.StockLiveData;

/**
 * 类描述：
 * 创建人：tgw
 * 创建时间：2020/8/6
 * 修改备注：
 */
public class CustomLiveDataFragment extends Fragment {
    public static final String TAG = CustomLiveDataFragment.class.getName();

    private FragmentStockLivedataBinding mBinding;
    private StockLiveData stockLiveData;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mBinding == null)
            mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_stock_livedata, container, false);
        return mBinding.getRoot();
    }


    //使用databin绑定点击事件
    public void doClick(View view) {
        stockLiveData.updataData();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //observer 当userinfo的数据发生改变时调用 下面的方法
        // Create the observer which updates the UI.  当
        final Observer<UserInfo> nameObserver = new Observer<UserInfo>() {
            @Override
            public void onChanged(@Nullable final UserInfo userInfo) {
                mBinding.tvName.setText(userInfo.getNickname());
            }
        };

        stockLiveData = StockLiveData.getStockLiveData();
        // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
        stockLiveData.observe(this, nameObserver);


        //databinding的绑定事件
        mBinding.setLiveDataFragment(CustomLiveDataFragment.this);
    }
}
